﻿#include "GlobalVar.h"
#include "JsonUtils.h"

GlobalVar::GlobalVar()
{
    QString initFile=QString("./Config/%1").arg("Config.ini");
    Setting=new QSettings(initFile,QSettings::IniFormat);
    Setting->setValue("sample","-----");
    Setting->sync();
}

GlobalVar &GlobalVar::Instance()
{
    static GlobalVar _instance;
    return _instance;
}

MeasureParam GlobalVar::GetMeasureParam()
{
    QSettings *set=Setting;
    MeasureParam ret;
    if(!set->contains("MeasureParam/SampleTimes"))
    {
        MeasureParam mp;
        SetMeasureParam(mp);
    }
    set->beginGroup("MeasureParam");
    ret.SampleTimes=set->value("SampleTimes").toInt();
    ret.SampleIntervalMs=set->value("SampleIntervalMs").toInt();
    ret.SaveDir=set->value("SaveDir").toString();
    ret.SampleMode=set->value("SampleMode").toString();
    ret.IsRemoveBaseline=set->value("IsRemoveBaseline").toBool();
    ret.IsRemoveBlankLine=set->value("IsRemoveBlankLine").toBool();
    set->endGroup();
    return ret;
}

void GlobalVar::SetMeasureParam(MeasureParam param)
{
    QSettings *set=Setting;
    set->beginGroup("MeasureParam");
    set->setValue("SampleTimes",param.SampleTimes);
    set->setValue("SampleIntervalMs",param.SampleIntervalMs);
    set->setValue("SaveDir",param.SaveDir);
    set->setValue("SampleMode",param.SampleMode);
    set->setValue("IsRemoveBaseline",param.IsRemoveBaseline);
    set->setValue("IsRemoveBlankLine",param.IsRemoveBlankLine);
    set->endGroup();
    set->sync();
}

ValidPw GlobalVar::GetPw()
{
    QSettings *set=Setting;
    ValidPw ret;
    if(!set->contains("ValidPw/Password"))
    {
        ValidPw mp;
        SetPw(mp);
    }
    set->beginGroup("ValidPw");
    ret.Password=set->value("Password").toString();
    set->endGroup();
    return ret;
}

void GlobalVar::SetPw(ValidPw param)
{
    QSettings *set=GlobalVar::Instance().Setting;
    set->beginGroup("ValidPw");
    set->setValue("Password",param.Password);
    set->endGroup();
}

void GlobalVar::SetPlotConfig(bool lockAxis,double upper)
{
    QSettings *set=GlobalVar::Instance().Setting;
    set->beginGroup("PlotConfig");
    set->setValue("LockAxis",lockAxis);
    set->setValue("Upper",upper);
    set->endGroup();
}

void GlobalVar::GetPlotConfig(bool &lockAxis,double &upper)
{
    QSettings *set=GlobalVar::Instance().Setting;
    set->beginGroup("PlotConfig");
    lockAxis=set->value("LockAxis",false).toBool();
    upper=set->value("Upper",2000.0).toDouble();
    set->endGroup();
}

